/**
 * 
 */
package kr.co.insoft.driver.amazon.model;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import kr.co.insoft.driver.model.InstanceModel;
import kr.co.insoft.driver.model.InstanceResultModel;

import com.amazonaws.services.ec2.model.GroupIdentifier;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.Reservation;
import com.amazonaws.services.ec2.model.Tag;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
public class AmazonInstanceResultModel extends InstanceResultModel {

	private static final Logger logger = LoggerFactory
			.getLogger(AmazonInstanceResultModel.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 4772438416456167156L;

	public AmazonInstanceResultModel(Reservation reservation) {
		setGroupNames(reservation.getGroupNames());
		setOwnerId(reservation.getOwnerId());
		setRequestId(reservation.getRequesterId());
		setReservationId(reservation.getReservationId());
		List<AmazonGroupModel> groups = new ArrayList<AmazonGroupModel>();
		setGroups(groups);
		for (GroupIdentifier group : reservation.getGroups()) {
			groups.add(new AmazonGroupModel(group));
		}
		setGroups(groups);

		List<AmazonInstanceModel> instances = new ArrayList<AmazonInstanceModel>();
		setInstances(instances);
		AmazonInstanceModel instanceTempModel = null;
		for (Instance instance : reservation.getInstances()) {
			instanceTempModel = new AmazonInstanceModel(instance);
			for (Tag tag : instance.getTags()) {
				if ("Name".equals(tag.getKey())) {
					instanceTempModel.setInstanceName(tag.getValue());
				}
			}
			instances.add(instanceTempModel);
		}
		setInstances(instances);
	}

	public AmazonInstanceResultModel(Reservation reservation,
			InstanceModel instanceModel) {
		setGroupNames(reservation.getGroupNames());
		setOwnerId(reservation.getOwnerId());
		setRequestId(reservation.getRequesterId());
		setReservationId(reservation.getReservationId());
		List<AmazonGroupModel> groups = new ArrayList<AmazonGroupModel>();
		setGroups(groups);
		for (GroupIdentifier group : reservation.getGroups()) {
			groups.add(new AmazonGroupModel(group));
		}
		setGroups(groups);

		List<AmazonInstanceModel> instances = new ArrayList<AmazonInstanceModel>();
		setInstances(instances);

		AmazonInstanceModel instanceTempModel = null;
		for (Instance instance : reservation.getInstances()) {
			logger.debug("{}", instance); 
			instanceTempModel = new AmazonInstanceModel(instance);
			instanceTempModel.setInstanceName(instanceModel.getInstanceName());
			instances.add(instanceTempModel);
		}
		logger.debug("{}", instances);
		setInstances(instances);
	}
}
